﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace community.Controllers
{
    [Route("api/complaints")]
    [ApiController]
    public class ComplaintsController : ControllerBase
    {
        private static List<Complaint> complaints = new List<Complaint>();

        // 提交投诉建议
        [HttpPost]
        public IActionResult SubmitComplaint([FromBody] Complaint newComplaint)
        {
            newComplaint.Id = complaints.Count + 1;
            newComplaint.SubmittedAt = DateTime.UtcNow;
            newComplaint.Status = "Pending";
            complaints.Add(newComplaint);
            return CreatedAtAction(nameof(GetComplaintById), new { id = newComplaint.Id }, newComplaint);
        }

        // 获取所有投诉建议
        [HttpGet]
        public IActionResult GetAllComplaints()
        {
            return Ok(complaints);
        }

        // 根据ID查询投诉建议
        [HttpGet("{id}")]
        public IActionResult GetComplaintById(int id)
        {
            var complaint = complaints.FirstOrDefault(c => c.Id == id);
            if (complaint == null)
            {
                return NotFound();
            }
            return Ok(complaint);
        }

        // 根据用户查询投诉建议
        [HttpGet("user/{userId}")]
        public IActionResult GetComplaintsByUser(int userId)
        {
            var userComplaints = complaints.Where(c => c.UserId == userId).ToList();
            return Ok(userComplaints);
        }

        // 处理投诉建议（修改状态、添加反馈）
        [HttpPut("{id}")]
        public IActionResult ProcessComplaint(int id, [FromBody] Complaint updatedComplaint)
        {
            var complaint = complaints.FirstOrDefault(c => c.Id == id);
            if (complaint == null)
            {
                return NotFound();
            }

            complaint.Status = updatedComplaint.Status;
            complaint.Response = updatedComplaint.Response;
            complaint.ProcessedAt = DateTime.UtcNow;

            return NoContent();
        }

        // 删除投诉建议
        [HttpDelete("{id}")]
        public IActionResult DeleteComplaint(int id)
        {
            var complaint = complaints.FirstOrDefault(c => c.Id == id);
            if (complaint == null)
            {
                return NotFound();
            }

            complaints.Remove(complaint);
            return NoContent();
        }
    }
}
